<?php

/**
 * Description of CategoryController
 * 
 * @author trongkhoi
 * @since Jul 30, 2013 - 3:21:32 PM
 */
class CategoryController extends MyController
{

    public function actionIndex()
    {

        $this->checkAccess();

        $cri             = new CDbCriteria();
        $count           = Category::model()->count($cri);
        $pages           = new CPagination($count);
        $pages->route    = "category/index";
        $pages->pageSize = $this->currentLimit;
        $pages->applyLimit($cri);
        $model           = Category::model()->findAll($cri);

        $this->render('index', array(
            'model' => $model,
            'pages' => $pages,
        ));
    }

    public function actionDelete()
    {
        $this->checkAccess();
        //Delete category
        if (isset($_GET['pk']))
        {
            if (Category::model()->deleteByPk($_GET['pk']))
            {
                Yii::app()->user->setFlash('Success_delete', 'Category deleted!');
                $this->redirect($this->createUrl('category/index', array(
                            'page' => @$_GET['page'],
                )));
            }
        }
        //Delete group
        if (isset($_GET['id']))
        {
            $model_group = CategoryGroup::model()->findByPk($_GET['id']);
            if ($model_group->delete())
            {
                Category::model()->updateAll(array('group_id' => 1), 'group_id=' . $_GET['id']);
                Yii::app()->user->setFlash('Success_delete', 'Category group deleted!');
                $this->redirect($this->createUrl('category/index', array(
                            'page' => @$_GET['page'],
                )));
            }
        }
    }

    public function actionEdit()
    {
        $this->checkAccess();

        $pk = isset($_GET['pk']) ? $_GET['pk'] : FALSE;

        $model = Category::model()->findByPk($pk);
        if ($model == null)
            throw new CHttpException(404, Yii::t('yii', 'Category not found!'));

        if (isset($_POST['Category']))
        {
            $model->attributes = $_POST['Category'];
            if (!$model->validate())
            {
                Yii::app()->user->setFlash('Error_edit', 'Please fill correct information !');
            }
            else
            {
                if ($model->save())
                {
                    // update avatar mới cho service

                    $firstAvarta = $this->_uploadPhoto('category', 120, 120, 'CateAvatar');
                    if ($firstAvarta && !$model->avatar)
                    {
                        $model->avatar = $firstAvarta;
                        $model->update();
                    }
                    if ($firstAvarta && $model->avatar != null)
                    {
                        if ($firstAvarta != $model->avatar)
                        {
                            $model->avatar = $firstAvarta;
                            $model->update();
                        }
                    }
                    //delete avatar trong service
                    if (isset($_POST['delCatAvatar']))
                    {
                        foreach ($_POST['delCatAvatar'] as $catId)
                        {
                            $svAvarta = Category::model()->findByPk($catId);
                            if ($svAvarta != null)
                            {
                                @unlink($svAvarta->avatar);
                            }
                        }
                    }

                    Yii::app()->user->setFlash(
                            'Success_edit', "Updated category successfully !"
                    );
                    $this->redirect($this->createUrl('category/index'));
                }
                else
                {
                    Yii::app()->user->setFlash('Error_edit', 'Please fill correct information !');
                }
            }
        }
        $this->render('_form_category', array('model' => $model));
    }

    public function actionCreate()
    {
        $this->checkAccess();

        $model = new Category;
        if (isset($_POST['Category']))
        {
            $model->attributes = $_POST['Category'];
            if (!$model->validate())
            {
                Yii::app()->user->setFlash('Error_edit', 'Please fill correct information !');
            }
            else
            {
                // thêm avatar mới cho category
                $firstAvarta = $this->_uploadPhoto('category', 120, 120, 'CateAvatar');
                if ($firstAvarta && !$model->avatar)
                {
                    $model->avatar = $firstAvarta;
                }
                if ($model->save())
                {
                    $this->redirect($this->createUrl('category/index'));
                }
                else
                {
                    Yii::app()->user->setFlash('Error_edit', 'Please fill correct information !');
                }
            }
        }

        $this->render('_form_category', array('model' => $model));
    }

    public function actionCreateGroup()
    {
        $this->checkAccess();

        $model = new CategoryGroup;
        if (isset($_POST['CategoryGroup']))
        {
            $model->attributes = $_POST['CategoryGroup'];
            if ($model->validate() && $model->save())
                $this->redirect($this->createUrl('category/index'));
            else
                Yii::app()->user->setFlash('Error_create', 'Please fill correct information !');
        }
        $this->render('_form_group', array('model' => $model));
    }

    public function actionEditgroup()
    {
        $this->checkAccess();

        $pk = isset($_GET['pk']) ? $_GET['pk'] : FALSE;

        $model = CategoryGroup::model()->findByPk($pk);
        if ($model == null)
            throw new CHttpException(404, Yii::t('yii', 'Category not found!'));

        if (isset($_POST['CategoryGroup']))
        {
            $model->attributes = $_POST['CategoryGroup'];
            if (!$model->validate())
            {
                Yii::app()->user->setFlash('Error_edit', 'Please fill correct information !');
            }
            else
            {

                if ($model->save())
                {
                    Yii::app()->user->setFlash(
                            'Success_edit', "Updated category successfully !"
                    );
                    $this->redirect($this->createUrl('category/index'));
                }
                else
                {
                    Yii::app()->user->setFlash('Error_edit', 'Please fill correct information !');
                }
            }
        }
        $this->render('_form_group', array('model' => $model));
    }

}

/**
 * End of CategoryController
 */